package cn.wbnull.springbootdemo.xiancehng;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 功能描述
 *
 * @author EDZ
 * @since 2022-08-16
 */
public class ThreadAccount extends Thread {
    private Account account;

    public ThreadAccount() {

    }

    public ThreadAccount(Account account) {
        this.account = account;

    }

    public Account getAccount() {
        return account;

    }

    public void setAccount(Account account) {
        this.account = account;
    }

    private Lock lock = new ReentrantLock();

    @Override
    public void run() {

        Thread thread = Thread.currentThread();
        lock.lock();
        if (account.getNum() > 0) {
            System.out.println(thread.getName() + "已经成功获得了这瓶水");
            account.setNum(account.getNum() - 1);
            System.out.println("此时还剩下" + account.getNum() + "瓶水");
        } else {
            System.out.println("水资源不足，请和尚蓄水");
        }
        lock.unlock();
    }

    //    public synchronized void water(){
//        if (account.getNum() > 0) {
////            System.out.println(thread.getName() + "已经成功获得了这瓶水");
//            account.setNum(account.getNum() - 1);
//            System.out.println("此时还剩下" + account.getNum() + "瓶水");
//
//        } else {
//            System.out.println("水资源不足，请和尚蓄水");
//        }
//    }
//    public static void main(String[] args) {
//        Account account = new Account(1);
//        ThreadAccount threadAccount1 = new ThreadAccount(account);
//        ThreadAccount threadAccount2 = new ThreadAccount(account);
//        Thread wna = new Thread(threadAccount1, "wna");
//        Thread 王志新 = new Thread(threadAccount2, "王志新");
//        wna.start();
//        王志新.start();
//    }

    //线程池

    public static void main(String[] args) {
        ThreadPoolExecutor pool = new ThreadPoolExecutor(2,4,10, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3), Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
        Runnable target = new MyThreadRunnable();
        //核心线程+任务队列=5，所以5个以内不需要创建临时线程
        pool.execute(target);
        pool.execute(target);
        pool.execute(target);
        pool.execute(target);
        pool.execute(target);
        //当任务数量达到5个的时候，接收任务的时候就需要创建临时线程
        pool.execute(target);
        pool.execute(target);
        //当超过7个线程时，再次接收任务时，则会拒绝
        pool.execute(target);
    }
    //实现Runnable接口
    public static class MyThreadRunnable implements Runnable{
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName()+"正在处理任务");
            try {
                Thread.sleep(10000000);//避免线程快速处理任务，无法达到实验效果
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


}
